package com.example.dzcfinal.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.dzcfinal.Index2;
import com.example.dzcfinal.Index3;
import com.example.dzcfinal.LoginActivity;
import com.example.dzcfinal.MainActivity;
import com.example.dzcfinal.NextActivity;
import com.example.dzcfinal.NoticeActivity;
import com.example.dzcfinal.R;
import com.example.dzcfinal.Three;


public class  HomeFragment extends Fragment {
    private Button enterLogin;
    private Button loginOut;
    private Integer userId;
    private LinearLayout one;
    private LinearLayout two;
    private LinearLayout three;
    private LinearLayout cang;
    private LinearLayout notice;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        //执行函数
        initHandle(view);
        EnterLogin();
        LoginOut();


        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 如果没有用户登录userID是-1,如果不等于-1说明是登录状态
                if (userId!=-1){
                    Intent intent=new Intent(getActivity(), NextActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(getActivity(), "请先登录再购买", Toast.LENGTH_SHORT).show();
                }

            }
        });

        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                这里也是页面跳转出现错误
                if (userId!=-1){
                    Intent intent = new Intent(getActivity(), Index2.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(getActivity(), "请先登录再购买", Toast.LENGTH_SHORT).show();
                }

            }
        });

        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userId!=-1){
                    Intent intent = new Intent(getActivity(), Index3.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(getActivity(), "请先登录再购买", Toast.LENGTH_SHORT).show();
                }

            }
        });
        cang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                这是跳转藏品
                if (userId!=-1){
                    Intent intent = new Intent(getActivity(), Three.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(getActivity(), "请先登录再查看藏品", Toast.LENGTH_SHORT).show();
                }

            }
        });
        notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), NoticeActivity.class);
                startActivity(intent);
            }
        });


        //判断是否有用户登录
        userId = ((MainActivity)getActivity()).getUserId();
        Log.i("TAG", userId.toString());

        if (userId!=-1){
            enterLogin.setVisibility(View.INVISIBLE);
            loginOut.setVisibility(View.VISIBLE);
        }


        return view;
    }


    private void initHandle(View view){
        enterLogin =  view.findViewById(R.id.enterLogin);
        loginOut = view.findViewById(R.id.loginOut);
        one = view.findViewById(R.id.one);
        two = view.findViewById(R.id.two);
        three = view.findViewById(R.id.three);
        cang = view.findViewById(R.id.mian_yao);
        notice = view.findViewById(R.id.mian_gao);
    }

    private void EnterLogin(){
        enterLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent1);
            }
        });
    }

    private void LoginOut(){
        loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((MainActivity)getActivity()).setUserId(-1);
                enterLogin.setVisibility(View.VISIBLE);
                loginOut.setVisibility(View.INVISIBLE);
                Toast.makeText(getActivity(), "退出登录成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

}